//
//  PointHistoryVC.m
//  xychain_DM_iOS
//
//  Created by Jpwang on 2018/6/11.
//  Copyright © 2018年 Jpwang. All rights reserved.
//

#import "PointHistoryVC.h"
#import "HistoryItemCell.h"

@interface PointHistoryVC () <UITableViewDelegate, UITableViewDataSource> {
    NSInteger mStart;
}

@property(weak, nonatomic)IBOutlet UIButton *backBt;
@property(weak, nonatomic)IBOutlet UITableView *tableView;

@property(strong,nonatomic)NSMutableArray *dataSource;

@end

@implementation PointHistoryVC

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self initView];
    [self requestData];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)initView {
    self.dataSource = [[NSMutableArray alloc] init];
    [[self.backBt rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(id x) {
        [self.navigationController popViewControllerAnimated:YES];
    }];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    [self.tableView registerNib:[UINib nibWithNibName:@"HistoryItemCell" bundle:nil] forCellReuseIdentifier:@"HistoryItemCellID"];
    self.tableView.mj_header = [MJRefreshStateHeader headerWithRefreshingBlock:^{
        self->mStart = 0;
        [self requestData];
    }];
    self.tableView.mj_footer = [MJRefreshFooter footerWithRefreshingBlock:^{
        self->mStart = self.dataSource.count;
        [self requestData];
    }];
}

- (void)requestData {
    [self showLoading:@"加载中"];
    NSDictionary *dic = @{@"start": [NSNumber numberWithInteger:mStart], @"length": [NSNumber numberWithInt:mLength]};
    [[[HttpRequest shareInstance] signGet:w_point_history map:[NSMutableDictionary dictionaryWithDictionary:dic]] subscribeNext:^(id x) {
        [self hideLoading];
        [self.tableView.mj_header endRefreshing];
        [self.tableView.mj_footer endRefreshing];
        if ([x[@"code"] intValue] == 200) {
            NSError *error;
            NSArray *temp = [MTLJSONAdapter modelsOfClass:[PointHistory class] fromJSONArray:x[@"data"] error:&error];
            if (!error) {
                if (self->mStart == 0) {
                    [self.dataSource removeAllObjects];
                }
                if (self.dataSource.count<mLength) {
                    [self.tableView.mj_footer endRefreshingWithNoMoreData];
                }
                [self.dataSource addObjectsFromArray:temp];
                [self.tableView reloadData];
            } else {
                [JPToast showText:@"数据解析错误"];
            }
        } else {
            [JPToast showText:x[@"message"]];
        }
    } error:^(NSError *error) {
        [self hideLoading];
        [self.tableView.mj_header endRefreshing];
        [self.tableView.mj_footer endRefreshing];
        [JPToast showText:@"网络错误"];
    }];
}

#pragma mark UITableView delegate and datasource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.dataSource.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (0<= indexPath.row && indexPath.row<self.dataSource.count) {
        PointHistory *model = self.dataSource[indexPath.row];
        if ([model.deltaType intValue] != 1 && model.businessId && model.businessId.length>0) {
            return 100;
        } else {
            return 80;
        }
    }
    return 0;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    HistoryItemCell *cell = [tableView dequeueReusableCellWithIdentifier:@"HistoryItemCellID"];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    [cell performSelector:@selector(setModel:) withObject:self.dataSource[indexPath.row]];
    return cell;
}

- (UIStatusBarStyle)preferredStatusBarStyle {
    return UIStatusBarStyleLightContent;
}

@end
